package design_pattern.inversion;

/**
 * @Name : inversion.WebsocketTest
 * @Description : 描述
 * Created by cyd on 2020-12-14 10:14:42
 */
public class Test {
    public static void main(String[] args) {
        OpenAndClose openAndClose = new OpenAndClose(new XMTv());
        openAndClose.open();
    }
}

//IOpenAndClose是OpenAndClose的规范
interface IOpenAndClose {
    public void open();
}

//ITv是Tv的规范
interface ITv {
    void play();
}

class OpenAndClose implements IOpenAndClose {
    private ITv tv;

    //通过构造器传入ITv实例，这样OpenAndClose依然是依赖ITv
    public OpenAndClose(ITv tv) {
        this.tv = tv;
    }

    @Override
    public void open() { //在这里体现出接口传递依赖关系
        this.tv.play();
    }
}

class XMTv implements ITv {
    private final String name = "xiaomi";

    @Override
    public void play() {
        System.out.println(name + ":通过遥控打开电视");
    }
}
